package com.example.lenovo.test1;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher;

import java.util.ArrayList;

public class Main6Activity extends AppCompatActivity {

    private ArrayList<Drawable> mListDrawable;
    private ImageSwitcher mImageSwitcher;

    private Button mbtnPre;
    private Button mbtnNext;

    private int index = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main6);
        bindWidget();
        initData();
        initView();


    }

    protected void initData() {
        mListDrawable = new ArrayList<Drawable>();
        mListDrawable.add(getResources().getDrawable(R.mipmap.person_run1));
        mListDrawable.add(getResources().getDrawable(R.mipmap.person_run2));
        mListDrawable.add(getResources().getDrawable(R.mipmap.person_run3));
        mListDrawable.add(getResources().getDrawable(R.mipmap.person_run4));
        mListDrawable.add(getResources().getDrawable(R.mipmap.person_run5));
    }


    protected void bindWidget() {
        mImageSwitcher = findViewById(R.id.imageSwitcher);
        mbtnPre = findViewById(R.id.btnPre);
        mbtnNext = findViewById(R.id.btnNext);
    }
    protected void initView() {
        mbtnPre.setOnClickListener(listener);
        mbtnNext.setOnClickListener(listener);
        mImageSwitcher.setInAnimation(AnimationUtils.loadAnimation(Main6Activity.this, android.R.anim.slide_in_left));
        mImageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(Main6Activity.this, android.R.anim.slide_out_right));
        mImageSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
            @Override
            public View makeView() {
                return new ImageView(Main6Activity.this); // makeView返回的是当前需要显示的ImageView控件，用于填充进ImageSwitcher中。
            }
        });
        mImageSwitcher.setImageDrawable(mListDrawable.get(0));
    }
    private View.OnClickListener listener=new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.btnPre:
                    index--;
                    if (index < 0)
                        index = mListDrawable.size() - 1;//用于循环显示图片
                    mImageSwitcher.setImageDrawable(mListDrawable.get(index)); //设定ImageSwitcher显示新图片
                    break;
                case R.id.btnNext:
                    index++;
                    if (index >= mListDrawable.size())
                        index = 0; //用于循环显示图片
                    mImageSwitcher.setImageDrawable(mListDrawable.get(index));
                    break;
                default:
                    break;
            }
        }
    };
}


